package xps.and.uudaijia.userclient.data.net;

import android.content.SharedPreferences;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.jude.utils.JUtils;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import xps.and.uudaijia.userclient.App;

/**
 * Created by XPS on 2017/7/17.
 */

public class LoggingInterceptor implements Interceptor {
    String str = "1";

    @Override
    public Response intercept(@Nullable Chain chain) throws IOException {
        Request request = chain.request();
        Request.Builder builder = request.newBuilder();
        SharedPreferences sp = JUtils.getSharedPreference();
        String AccessToken = sp.getString("User_Access_token", "");
        if (!TextUtils.isEmpty(AccessToken)) {
            str = "1";
            builder.url(request.url().toString() + "?access-token=" + AccessToken);
        } else {
            builder.url(request.url().toString());
        }
        Response resp = chain.proceed(builder.build());
        if (!TextUtils.isEmpty(AccessToken)) {
            if (resp.code() == 401) {
                if (str.equals("1")) {
                    App.getInstance().exitApp();
                    str = "2";
                }
            }
        }
        return resp;
    }

}
